import { IllegalArgumentException } from "../Error/error"

export class TimeUnit {
  private readonly timeUnitName : string
  private constructor(name: string) {
    this.timeUnitName = name
  }

  static NANOSECONDS = TimeUnit.forName("NANOSECONDS")
  static MICROSECONDS = TimeUnit.forName("MICROSECONDS")
  static MILLISECONDS = TimeUnit.forName("MILLISECONDS")
  static SECONDS = TimeUnit.forName("SECONDS")
  static MINUTES = TimeUnit.forName("MINUTES")
  static HOURS = TimeUnit.forName("HOURS")
  static DAYS = TimeUnit.forName("DAYS")

  private getSecondsPerUnit(): number {
    switch (this.timeUnitName) {
      case "NANOSECONDS":   return 1e-9;        // 1 ns = 10^-9 s
      case "MICROSECONDS":  return 1e-6;        // 1 µs = 10^-6 s
      case "MILLISECONDS":  return 1e-3;        // 1 ms = 10^-3 s
      case "SECONDS":       return 1;
      case "MINUTES":       return 60;
      case "HOURS":         return 60 * 60;     // 3600
      case "DAYS":          return 24 * 60 * 60; // 86400
      default:
        throw new IllegalArgumentException(`Unknown time unit: ${this.timeUnitName}`);
    }
  }

  private getMillisPerUnit(): number {
    // 1 秒 = 1000 毫秒
    return this.getSecondsPerUnit() * 1000;
  }

  static forName(name: string): TimeUnit {
    return new TimeUnit(name)
  }

  toMillis(num :number): number{
    return num * this.getMillisPerUnit();
  }

  toSeconds(num :number): number{
    return num * this.getSecondsPerUnit();
  }

  toString(): string {
    return this.timeUnitName
  }

  name(): string {
    return this.timeUnitName
  }

}